package com.lg.my.netty.codec;

import com.lg.netty.codec.ProtostuffUtil;
import com.lg.netty.codec.User;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * 自定义 编码
 *
 * @author by Mr. Li 2021/4/22 6:31
 */
public class ObjectDecodeByProtocl<T> extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        byte[] bytes = new byte[in.readableBytes()];
        if (bytes.length != 0) {
            // 读取 bytebuf中的数据，将数据读取到byte数组中
            in.readBytes(bytes);

            out.add(ProtostuffUtil.deserializer(bytes, User.class));
        }

    }
}
